Repository navigation
fix(serializers): prevent AttributeError and ObjectDoesNotExist in PluginConfigSerializer secret redaction. Closes #4052 - #4053
Conversation
|
Ready for maintainer review whenever convenient. All CI checks pass and the branch is clean and up to date with develop. |
fbf6847 to
1e86300
Compare
|
Hi, thanks for the patience and yes, absolutely. Here are the regression tests I promised on #4052, in diff form so you can fold them straight into this branch. They cover the cases we agreed on: reading a secret organization value as the owner and as an organization admin of that org (both allowed), as a regular member of the org and as a user of another org (both redacted), an owner whose membership was removed after the config was created, an organization config with a null owner that can only appear through bulk writes such as bulk_create, and serializing without a request or with an unauthenticated user. I ran this module green against my local docker stack when I wrote these; since your branch implements the same null-safe logic, they should apply cleanly, though please treat a run on your branch as the source of truth. If anything trips up I will debug it here with you. Feel free to add me as a co-author when folding them in, and I am happy to review the serializer patch itself while it stays open. diff --git a/tests/api_app/test_serializers.py b/tests/api_app/test_serializers.py
index 98c6b0ab..89595109 100644
--- a/tests/api_app/test_serializers.py
+++ b/tests/api_app/test_serializers.py
@@ -85,6 +85,113 @@ class PluginConfigSerializerTestCase(CustomTestCase):
org.delete()
pc.delete()
+ def test_to_representation_secret_for_organization(self):
+ org = Organization.objects.create(name="test_org")
+ other_org = Organization.objects.create(name="other_org")
+ owner = User.objects.create_user(
+ username="config_owner",
+ email="config_owner@intelowl.com",
+ password="test",
+ )
+ Membership.objects.create(user=owner, organization=org, is_owner=True)
+ Membership.objects.create(user=self.admin, organization=org, is_owner=False, is_admin=True)
+ Membership.objects.create(user=self.user, organization=org, is_owner=False, is_admin=False)
+ Membership.objects.create(user=self.guest, organization=other_org, is_owner=True)
+ ac = AnalyzerConfig.objects.get(name="AbuseIPDB")
+ param = Parameter.objects.create(
+ is_secret=True,
+ name="mynewparameter",
+ python_module=ac.python_module,
+ required=True,
+ type="str",
+ )
+ pc = PluginConfig.objects.create(
+ value="supersecret",
+ owner=owner,
+ parameter=param,
+ analyzer_config=ac,
+ for_organization=True,
+ )
+
+ def get_value(user):
+ return PluginConfigSerializer(
+ pc, context={"request": MockUpRequest(user=user)}
+ ).data["value"]
+
+ # the owner and an admin of the owner's organization can read the value
+ self.assertEqual(get_value(owner), "supersecret")
+ self.assertEqual(get_value(self.admin), "supersecret")
+ # other members of the organization and users of other organizations cannot
+ self.assertEqual(get_value(self.user), "redacted")
+ self.assertEqual(get_value(self.guest), "redacted")
+
+ # an owner whose membership was removed after the config was created
+ # (org deleted / member removed) - the realistic way this state occurs
+ orphan_owner = User.objects.create_user(
+ username="orphan_owner",
+ email="orphan_owner@intelowl.com",
+ password="test",
+ )
+ Membership.objects.create(
+ user=orphan_owner,
+ organization=Organization.objects.create(name="orphan_org"),
+ is_owner=True,
+ )
+ pc_no_membership = PluginConfig.objects.create(
+ value="supersecret",
+ owner=orphan_owner,
+ parameter=param,
+ analyzer_config=ac,
+ for_organization=True,
+ )
+ orphan_owner.membership.delete()
+ self.assertEqual(
+ PluginConfigSerializer(
+ pc_no_membership,
+ context={"request": MockUpRequest(user=self.admin)},
+ ).data["value"],
+ "redacted",
+ )
+ # the owner can still read their own value
+ self.assertEqual(
+ PluginConfigSerializer(
+ pc_no_membership,
+ context={"request": MockUpRequest(user=orphan_owner)},
+ ).data["value"],
+ "supersecret",
+ )
+
+ # an owner-less org configuration can only arise through bulk writes
+ # (bulk_create bypasses save/clean) - serializing it must not raise
+ pc_no_owner = PluginConfig.objects.bulk_create(
+ [
+ PluginConfig(
+ value="supersecret",
+ owner=None,
+ parameter=param,
+ analyzer_config=ac,
+ for_organization=True,
+ )
+ ]
+ )[0]
+ self.assertEqual(
+ PluginConfigSerializer(
+ pc_no_owner,
+ context={"request": MockUpRequest(user=self.admin)},
+ ).data["value"],
+ "redacted",
+ )
+
+ # missing request or request user also results in a redacted value
+ self.assertEqual(PluginConfigSerializer(pc_no_owner).data["value"], "redacted")
+ self.assertEqual(
+ PluginConfigSerializer(
+ pc_no_owner,
+ context={"request": MockUpRequest(user=None)},
+ ).data["value"],
+ "redacted",
+ )
+
def test_create(self):
org = Organization.objects.create(name="test_org")
m1 = Membership.objects.create(user=self.user, organization=org, is_owner=True) |
…uginConfigSerializer secret redaction. Closes intelowlproject#4052 Secret redaction in PluginConfigSerializer dereferenced instance.owner and the owner's membership without checking them, so a config with no owner raised AttributeError and a config whose owner no longer has a membership raised ObjectDoesNotExist, both ending in a 500. Check the request user, the owner and both memberships before deciding whether to redact, and compare organization_id instead of loading the organizations. OwnershipAbstractModel.organization now returns None when the owner is missing or has no membership, and to_representation falls back to None for the organization name. Add regression tests covering secret redaction for organization configs, bulk-created owner-less configs, membership deletion post-creation, and missing request context. Co-authored-by: asucodes <asucodes@users.noreply.github.com> Signed-off-by: Harshit Kudhial <harshitkudhial@gmail.com>
1e86300 to
6a41065
Compare
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
Hey @asucodes, Thanks for providing the test suite! I've added the regression tests for the branch and added you as a co-author for the commit (Co-authored-by: asucodes ).the use of bulk_create to test the owner-less organization configuration without triggering model clean() is a great find! The tests are passing and all linters (ruff) are passing with no errors. If you get a chance, you can take a look at the updated serializer patch. Thanks for working with me! |
|
This pull request has been marked as stale because it has had no activity for 10 days. If you are still working on this, please provide some updates or it will be closed in 5 days. |
|
This pull request has been closed because it had no updates in 15 days. If you're still working on this fell free to reopen. |
Description
When serializing a
PluginConfigwithis_secret=Trueandfor_organization=True,PluginConfigSerializer.CustomValueField.get_attributedirectly evaluatedinstance.owner.pkandinstance.owner.membership.organization.pk.This led to unhandled 500 exceptions in two scenarios:
AttributeError: 'NoneType' object has no attribute 'pk'wheninstance.ownerisNone(which is supported in the database constraints underapi_app/models.py).ObjectDoesNotExist: User has no membershipwheninstance.owneris set but lacks an active membership (e.g. users removed from an organization after the config was saved).requestor an authenticated user is missing from serializer context.This PR adds safe guards in
PluginConfigSerializer.CustomValueField.get_attribute(api_app/serializers/plugin.py):user.is_authenticatedandinstance.owneris notNonebefore comparinguser.pk == instance.owner.pk.user.has_membership(),user.membership.is_admin,instance.owner.has_membership(), and comparesorganization_iddirectly using the FK column — avoiding any.membershipattribute traversal that can raiseRelatedObjectDoesNotExist.PluginConfigSerializerTestCasecovering: global null-owner secrets, post-creation membership removal, and cross-organization visibility.Closes #4052
Type of change
Checklist
developdumpplugincommand and added it in the project as a data migration. ("How to share a plugin with the community")test_files.zipand you added the default tests for that mimetype in test_classes.py.FREE_TO_USE_ANALYZERSplaybook by following this guide.urlthat contains this information. This is required for Health Checks (HEAD HTTP requests).get_mocker_response()method of the unittest class. This serves us to provide a valid sample for testing.DataModelfor the new analyzer following the documentation# This file is a part of IntelOwl https://github.com/intelowlproject/IntelOwl # See the file 'LICENSE' for copying permission.Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.testsfolder). All the tests (new and old ones) gave 0 errors.DeepSource,Django Doctorsor other third-party linters have triggered any alerts during the CI checks, I have solved those alerts.Note on LLM assistance: An LLM was used to assist with static code analysis and drafting tests. All changes have been manually reviewed and verified against repository standards.